package com.ruoyi.web.controller.spoken;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SpokenTeacher;
import com.ruoyi.system.domain.SpokenTeacherCourseTime;
import com.ruoyi.system.domain.SpokenUserCourseTime;
import com.ruoyi.system.service.ISpokenTeacherCourseTimeService;
import com.ruoyi.system.service.ISpokenTeacherService;
import com.ruoyi.system.service.ISpokenUserCourseTimeService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(description = "教师管理")
@RequestMapping("/spoken/teacher")
@RestController
public class TeacherController extends BaseController {

    @Autowired
    ISpokenTeacherService teacherService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    SysLoginService loginService;

    @Autowired
    TokenService tokenService;

    @Autowired
    ISpokenTeacherCourseTimeService teacherCourseTimeService;

    @Autowired
    ISpokenUserCourseTimeService userCourseTimeService;

    @ApiOperation("列表查询")
    @GetMapping("/list")
    public TableDataInfo list(SpokenTeacher teacher){

        startPage();
        return getDataTable(teacherService.selectList(teacher));
    }

    @ApiOperation("新建教师")
    @PostMapping
    public AjaxResult add(@RequestBody SpokenTeacher teacher){
        // 校验用户名是否唯一
        boolean unique = sysUserService.checkUserNameUnique(teacher.getSysUser());
        if(!unique){
            return error("新增教师'" + teacher.getSysUser().getNickName() + "'失败，手机号已存在");
        }
        return toAjax(teacherService.insert(teacher));
    }

    @ApiOperation("根据教师ID查询教师")
    @GetMapping("/getById/{teacherId}")
    public AjaxResult getById(@PathVariable Long teacherId){
        return success(teacherService.selectById(teacherId));
    }

    @ApiOperation("查询教师信息")
    @GetMapping("/mobile")
    public AjaxResult mobileGetInfo(){
        Long teacherId = SecurityUtils.getUserId();
        return success(teacherService.selectById(teacherId));
    }

    @ApiOperation("修改教师信息")
    @PutMapping
    public AjaxResult edit(@RequestBody SpokenTeacher teacher){
        return success(teacherService.update(teacher));
    }

    @ApiOperation("修改会议信息")
    @PutMapping("/meeting")
    public AjaxResult editMeeting(@RequestBody SpokenTeacher teacher){
        return success(teacherService.updateMeetingInfo(teacher));
    }

    @ApiOperation("删除教师")
    @DeleteMapping("/{teacherId}")
    public AjaxResult remove(@PathVariable Long teacherId){
        return success(teacherService.delete(teacherId));
    }

    /**
     * 教师登录
     *
     * @return 结果
     */
    @ApiOperation("教师登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, String> loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String userName = loginBody.get("userName");
        String password = loginBody.get("password");
        // 登录
        String token = loginService.teacherLogin(userName,password);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     *
     * @param params
     * @return
     */
    @ApiOperation("根据年月查询当月教师没有课的日期")
    @PostMapping("/disabledDate")
    public AjaxResult getDisabledDate(@RequestBody Map params){
        List<String> disableDate = teacherService.selectDisableDate(Long.parseLong((String)params.get("teacherId")), (String) params.get("date"));
        return success(disableDate);
    }

    /**
     * 根据日期、教师ID查询对应时间段
     */
    @ApiOperation("根据日期、教师ID查询对应时间段")
    @PostMapping("/timeList")
    public AjaxResult getTimeList(@RequestBody Map params){
        List<SpokenTeacherCourseTime> timeList = teacherService.selectTimes(Long.parseLong((String)params.get("teacherId")), (String) params.get("date"));
        return success(timeList);
    }

    /**
     * 根据日期、教师ID查询对应时间段
     */
    @ApiOperation("查询教师关联课程表")
    @GetMapping("/courseTime")
    public AjaxResult getTeacherCourseTime(){
        Long teacherId = SecurityUtils.getUserId();
        List<SpokenTeacherCourseTime> spokenTeacherCourseTimes = teacherCourseTimeService.selectByTeacherId(teacherId);
        return success(spokenTeacherCourseTimes);
    }

    @ApiOperation("教师端：根据教师ID、开始日期、结束日期查询时间段内的课时选择情况")
    @PostMapping("/selectCourseTimeStatusByDate")
    public AjaxResult selectCourseTimeStatusByDate(@RequestBody Map params){
        Long teacherId = SecurityUtils.getUserId();
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        List<SpokenUserCourseTime> spokenUserCourseTimes = userCourseTimeService.selectCourseTimeStatusByDate(teacherId, startDate, endDate);
        return success(spokenUserCourseTimes);
    }

    @ApiOperation("教师端：查询选择该课时的学生信息")
    @GetMapping("/selectTipsInfo/{uctId}")
    public AjaxResult selectTipsInfo(@PathVariable Long uctId){
        return success(userCourseTimeService.selectTipsInfoByUctId(uctId));
    }

}
